﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using DAL;

namespace PeisongWeb.admin
{
    public partial class companyGroupList : System.Web.UI.Page
    {
        public int curCompanyId
        {
            get
            {
                int tmp=0;
                int.TryParse(ViewState["curCompanyId"].ToString(), out tmp);
                return tmp;
            }
        }

        public int curGroupId
        {
            get
            {
                int tmp = 0;
                int.TryParse(ViewState["curGroupId"].ToString(), out tmp);
                return tmp;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rptBind();
            }
        }

        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (txt_companyName.Text.Trim() != "")
                where.AppendFormat("and companyName like '%{0}%'", txt_companyName.Text.Trim());
            DataSet ds = CompanyGroup.select(where.ToString(), "companyName", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }
        
        protected void btnAddGroup_Click(object sender,EventArgs e)
        {
            txtGroupName.Clear();
            winGroup.Show();
        }

        // 保存分组信息
        protected void btnSaveGroup_Click(object sender,EventArgs e)
        {
            bool isAffected = CompanyGroup.Insert(txtGroupName.Text);
            if (isAffected)
            {
                Coolite.Ext.Web.Ext.Msg.Alert("提示", "保存成功!");
                winGroup.Hide();
            }
        }

        // 加入到分组
        protected void btnSaveCompanyGroup_Click(object sender,EventArgs e)
        {
            int newGroupId = 0;
            int.TryParse(cboCompanyGroup.SelectedItem.Value, out newGroupId);

            bool isAffected = CompanyGroup.insertUpdateRelation(curCompanyId, curGroupId, newGroupId);
            if (isAffected)
            {
                ViewState.Remove("curCompanyId");
                ViewState.Remove("curGroupId");

                winAddToGroup.Hide();

                rptBind();
            }
        }

        protected void rpt_ItemCommand(object sender,RepeaterCommandEventArgs e)
        {
            if (e.CommandName=="EditGroup")
            {
                string[] args = e.CommandArgument.ToString().Split(new char[] { ',' });
                ViewState["curCompanyId"] = args[0];
                ViewState["curGroupId"] = args[1];

                BindCompanyGroup();

                if (curGroupId != 0)
                {
                    cboCompanyGroup.SelectedItem.Value = curGroupId.ToString();
                }
                else
                {
                    cboCompanyGroup.SelectedItem.Value = null;
                }
                winAddToGroup.Show();
            }
        }

        private void BindCompanyGroup()
        {
            DataTable dt = CompanyGroup.listAll();
            stCommpanyGroup.DataSource = dt;
            stCommpanyGroup.DataBind();

            cboCompanyGroup.DisplayField = "groupName";
            cboCompanyGroup.ValueField = "groupId";
            cboCompanyGroup.DataBind();
        }
    }
}
